﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.JScript.Vsa;

/// <summary>
///strHelper 的摘要说明
/// </summary>
public class strHelper
{
    public strHelper()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 处理非数字和正无穷大
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    static public string checkCatch(string str)
    {
        string s;
        if (str == "非数字")
        { s = "0"; }
        else if (str == "正无穷大")
        { s = "0"; }
        else if (str == "负无穷大")
        { s = "0"; }
        else
        {
            s = str;
        }
        return s;
    }

    /// <summary>
    /// 判断某一年是否为闰年
    /// </summary>
    /// <param name="year"></param>
    /// <returns></returns>
    static public bool IsLeapYear(int year)
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
            return true;
        return false;
    }

    /// <summary>
    /// 计算方法
    /// </summary>
    /// <param name="formule">数字公式</param>
    /// <returns>Value(Type:string)</returns>
    static public string Cale(string formule)
    {
        try
        {
            Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
            return Microsoft.JScript.Eval.JScriptEvaluate(formule, ve).ToString();
        }
        catch (Exception ce)
        {
            return "0";
        }
    }

    /// <summary>
    /// 处理形如'{0}'，而参数中又有单引号的情况，将参数中的单引号double
    /// 如果args中的单引号不是作为文本的话，不要用这个方法，直接用string.format()
    /// </summary>
    /// <param name="format"></param>
    /// <param name="args"></param>
    /// <returns></returns>
    static public string stringFormat(string format, params object[] args)
    {
        if (format.IndexOf("'") == -1) //没有在单引号内的占位符
            return string.Format(format, args);

        ArrayList al = new ArrayList();
        for (int i = 0; i < args.Length; i++)
            al.Add(args[i].ToString().Replace("'", "''")); // 处理单引号
        return string.Format(format, al.ToArray());
    }

    /// <summary>
    /// 去除html超文本标记
    /// </summary>
    /// <param name="sCont"></param>
    /// <returns></returns>
    public static string ClearHtmlCont(string sCont)
    {
        int index = sCont.IndexOf("<");
        if (index >= 0)
        {
            int num2 = sCont.IndexOf(">");
            if (num2 > 0)
            {
                sCont = sCont.Remove(index, (num2 + 1) - index);
                return ClearHtmlCont(sCont);
            }
        }
        return sCont;
    }

    /// <summary>
    /// 字符串取到小数点后两位
    /// </summary>
    /// <param name="res"></param>
    /// <returns></returns>
    public static string ShowPointTwo(string res)
    {
        double val = 0;

        if (res == "" || res == null)
        {
            return "0";
        }
        else if (res == "&nbsp;")
        {
            return "&nbsp;";
        }
        else if (res == "0.00")
        {
            return "0";
        }
        else
        {
            try
            {
                val = Convert.ToDouble(res);
            }
            catch
            {
                val = 0;
            }
        }

        return val.ToString("f2");
    }

    public static string ShowPoint(string res, string num)
    {
        double val = 0;

        if (res == "" || res == null)
        {
            return "0";
        }
        else if (res == "0")
        {
            return "0";
        }
        else if (res == "&nbsp;")
        {
            return "&nbsp;";
        }
        else if (res == "0.00")
        {
            return "0";
        }
        else
        {
            try
            {
                val = Convert.ToDouble(res);
            }
            catch
            {
                val = 0;
            }
        }

        return val.ToString("f" + num);
    }

    /// <summary>
    /// 清除指定html超文本标记
    /// 注：区分大小写
    /// </summary>
    /// <param name="sCont">要清除的字符串</param>
    /// <param name="name">指定清除的标签名，如超连接 a </param>
    /// <returns></returns>
    public static string ClearHtmlCont(string sCont, string name)
    {
        int index = sCont.IndexOf(string.Format("<{0}", name));
        if (index >= 0)
        {
            int num2 = sCont.IndexOf(">", index);
            if (num2 > 0)
            {
                sCont = sCont.Remove(index, (num2 + 1) - index);
                return ClearHtmlCont(sCont, name);
            }
        }
        index = sCont.IndexOf(string.Format("</{0}", name));
        if (index >= 0)
        {
            int num2 = sCont.IndexOf(">", index);
            if (num2 > 0)
            {
                sCont = sCont.Remove(index, (num2 + 1) - index);
                return ClearHtmlCont(sCont, name);
            }
        }
        return sCont;
    }

    /// <summary>
    /// 将DataSet导出成CSV格式
    /// </summary>
    /// <param name="ds">DataSet</param>
    /// <returns>CSV字符串数据</returns>
    public static string ExportCSV(DataSet ds)
    {
        string data = "";
        //data = ds.DataSetName + "\n";

        foreach (DataTable tb in ds.Tables)
        {
            data += tb.TableName + "\n";

            //写出列名
            foreach (DataColumn column in tb.Columns)
            {
                data += column.ColumnName + ",";
            }
            data += "\n";

            //写出数据
            foreach (DataRow row in tb.Rows)
            {
                foreach (DataColumn column in tb.Columns)
                {
                    data += row[column].ToString() + ",";
                }
                data += "\n";
            }
            data += "\n";
        }

        return data;
    }

    /// <summary>
    /// 标记DataColumn为接受导出的
    /// </summary>
    /// <param name="column">DataColumn</param>
    public static void SetExport(DataColumn column)
    {
        if (column != null)
        {
            if (column.ExtendedProperties["IsExport"] == null)
                column.ExtendedProperties.Add("IsExport", "true");
            else
                column.ExtendedProperties["IsExport"] = "true";
        }
    }

    /// <summary>
    /// 标记DataTable中的一些列为接受导出的
    /// </summary>
    /// <param name="tb">DataTable</param>
    /// <param name="columns">列</param>
    public static void SetExport(DataTable tb, params string[] columns)
    {
        foreach (string column in columns)
        {
            SetExport(tb.Columns[column]);
        }
    }

    /// <summary>
    /// 标记DataTable中的一些列为接受导出的
    /// </summary>
    /// <param name="tb">DataTable</param>
    /// <param name="columns">DataGrid的列</param>
    public static void SetExport(DataTable tb, DataGridColumnCollection columns)
    {
        foreach (DataGridColumn column in columns)
        {
            if (column.GetType().Name == "BoundColumn" || column.GetType().Name == "HyperLinkColumn" || column.GetType().Name == "TemplateColumn")
                SetExport(tb.Columns[column.HeaderText]);
        }
    }

    /// <summary>
    /// 判断DataColumn是否为接受导出的
    /// </summary>
    /// <param name="column">DataColumn</param>
    /// <returns>bool型的结果</returns>
    public static bool IsExport(DataColumn column)
    {
        if (column.ExtendedProperties["IsExport"] != null && column.ExtendedProperties["IsExport"].ToString().Trim().ToLower() == "true")
            return true;
        else
            return false;
    }

   

}
